package com.sgb.kjwl.view.ui.activity.self.driver;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AppProprietaryDriverActivityBinding;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.fragment.self.ProprietaryDriverFragment;
import com.sgb.kjwl.viewmodel.self.driver.ProDriverListModel;
import com.sgb.link.router.PathDriver;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import java.util.ArrayList;

import static com.sgb.link.router.PathDriver.ProprietaryDriverActivity;

/**
 * Describe:    自营司机（列表页）
 * Author:  yangLei
 * Date:    2021/10/11
 */
@Route(path = ProprietaryDriverActivity)
public class ProprietaryDriverActivity extends BaseActivity<ProDriverListModel, AppProprietaryDriverActivityBinding> {
    @Autowired(name = RouterKey.TYPE, desc = "类型（0企业  1团队）")
    public String type;
    private String[] titles = {"全部", "已同意", "邀请中", "已拒绝", "已解除"};
    private SlidingFragmentPagerAdapter mAdapter;
    private ArrayList<Fragment> mFragments;
    public int currentChildTab;

    @Override
    protected int layoutId() {
        return R.layout.app_proprietary_driver_activity;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(ProDriverListModel.class);
    }

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        initData();
        initViews();
        initEvents();
    }

    private void initViews() {
        mBinding.includeTitle.titleView.setText("自营司机");
        mBinding.includeTitle.rightView.setText("+自营司机");
        mBinding.includeTitle.rightView.setOnClickListener(this::onClick);
        mBinding.includeTitle.ivBack.setOnClickListener(v -> finish());
    }

    private void initEvents() {
        mBinding.setOnClick(this);
        mBinding.includeTitle.ivBack.setOnClickListener(v -> finish());
    }

    private void initData() {
        if (mFragments == null) {
            mFragments = new ArrayList<>();
        }
        mFragments.clear();

        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("全部"));
        mFragments.add(ProprietaryDriverFragment.newInstance(0));

        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("已同意"));
        mFragments.add(ProprietaryDriverFragment.newInstance(3));

        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("邀请中"));
        mFragments.add(ProprietaryDriverFragment.newInstance(1));

        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("已拒绝"));
        mFragments.add(ProprietaryDriverFragment.newInstance(2));

        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("已解除"));
        mFragments.add(ProprietaryDriverFragment.newInstance(4));

        mAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        mBinding.viewPager.setCurrentItem(0);
        mBinding.viewPager.setOffscreenPageLimit(5);
        mBinding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mBinding.tabLayout));
        mBinding.viewPager.setAdapter(mAdapter);
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                mBinding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_view://去添加添加自营司机
                ARouter.getInstance().build(PathDriver.AddProprietaryDriverActivity)
//                        .withString(RouterKey.TYPE, type)
                        .navigation(this);
                break;
        }
    }
}
